﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using System.Data;

namespace Besture.UI
{
    public partial class UserBaseSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id == 0) return;
            var dac = new UserBaseDac();
            var model = dac.GetModel(id);
            if (model != null)
            {
                txtLoginName.Text = model.LoginName;
                txtNickName.Text = model.NickName;
                txtLoginPwd.Text = model.LoginPwd;
                txtPhone.Text = model.Phone;
                txtEmail.Text = model.Email;
                txtRealName.Text = model.RealName;
                ddlSex.SelectedValue = model.Sex.ToString() == "" ? "" : model.Sex.ToString();
                UploadSinglePhoto.ImageUrl = model.UserImage;
                txtBirthday.Text = Convert.ToDateTime(model.Birthday).ToString("yyyy-MM-dd");
                txtAreaCode.Text = model.AreaCode;
                txtTel.Text = model.Tel;
                txtTelExt.Text = model.TelExt;
                SelectCity.ProvinceID = model.ProvinceID;
                SelectCity.CityID = model.CityID;
                txtCompanyName.Text = model.CompanyName;
                txtRepresentative.Text = model.Representative;
                txtAddress.Text = model.Address;
                ddlUserRating.SelectedValue = model.UserRating.ToString();
                ddlRatingIntegration.SelectedValue = model.RatingIntegration.ToString();
                txtUserPoints.Text = model.UserPoints.ToString(); 
                hidUserTyle.Value = model.UserType.ToString();
                txtSortID.SortValue = model.SortID;
            }

        }
        #endregion

        #region 保存

        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {
            var dac = new UserBaseDac();
            UserBaseModel model = null;
            var baseDac = new BaseDac<UserBaseModel>();
            model = dac.GetModel(ID);
            model.LoginName = txtLoginName.Text.Trim();
            model.NickName = txtNickName.Text.Trim();
            model.LoginPwd = txtLoginPwd.Text.Trim();
            model.Phone = txtPhone.Text.Trim();
            model.Email = txtEmail.Text.Trim();
            model.RealName = txtRealName.Text.Trim();
            model.UserImage = UploadSinglePhoto.ImageUrl;
            model.Birthday = DateTime.Now; //txtBirthday.Text.Trim() == "" ? Convert.ToDateTime("0001/01/01") : Convert.ToDateTime(txtBirthday.Text.Trim());
            model.AreaCode = txtAreaCode.Text.Trim();
            model.Tel = txtTel.Text.Trim();
            model.TelExt = txtTelExt.Text.Trim();
            model.ProvinceID = Convert.ToInt32(SelectCity.ProvinceID);
            model.CityID = Convert.ToInt32(SelectCity.CityID);
            model.CompanyName = txtCompanyName.Text.Trim();
            model.Representative = txtRepresentative.Text.Trim();
            model.Address = txtAddress.Text.Trim();
            model.UserRating = ddlUserRating.SelectedValue.Length > 0 ? Convert.ToInt32(ddlUserRating.SelectedValue) : 0;
            model.RatingIntegration = ddlRatingIntegration.SelectedValue.Length > 0 ? Convert.ToInt32(ddlRatingIntegration.SelectedValue) : 0;
            model.UserPoints = Convert.ToInt32(txtUserPoints.Text);
            model.StatusTime = DateTime.Now;
            model.SortID = txtSortID.SortValue != 0 ? txtSortID.SortValue : 0;
            model.Sex = Convert.ToInt32(ddlSex.SelectedValue);
            baseDac.LoadUpdateBaseModel(model);
            if (dac.Update(model) > 0)
            {
                FunHelper.Alert("保存成功", ListUrl());
            }
        }
        #endregion
    }
}